<?php
    $this->headTitle(@$this->blog->title);

    $this->headLink()
    ->appendStylesheet($this->APP_STATIC_SERVER.'/js/jquery.ui/themes/base/ui.core.css')
    ->appendStylesheet($this->APP_STATIC_SERVER.'/js/jquery.ui/themes/base/ui.dialog.css')
    ->appendStylesheet($this->APP_STATIC_SERVER.'/js/jquery.ui/themes/base/ui.theme.css');

$this->headScript()
    ->appendFile($this->APP_STATIC_SERVER.'/js/jquery.ui/ui.core.js')
    ->appendFile($this->APP_STATIC_SERVER.'/js/jquery.ui/ui.dialog.js');
?>
<style>
    .b_breadcrumb{

    }
    .b_bottom{
        margin-bottom: 15px;
    }
    .b_a_right{
        text-align: right;
    }
    .b_l_content{
        display: inline-block;
    }
    .b_l_content .b_title{
        font-weight: bold;
        font-size: 10pt;
        color: #333366;
    }
    .b_l_content .b_post lable{
        font-weight: bold;
    }
    .b_l_content .b_content{
        text-align: justify;
        display: inline-block;
        width: 640px;
    }
    .b_l_content .b_action{
        margin: -5px 0 10px;
        padding-left: 100px;
    }

    *{margin:0;padding:0;}
    ol.timeline{
        list-style:none;font-size:1.2em;
    }
    ol.timeline li{
        position:relative;
        padding:.3em 0 .6em .3em;
        line-height:1.1em;
        background-color:#EFEDED;
        margin-bottom:3px;
        text-align:justify;
        display: inline-block;
        width: 97.5%
    }
    .panel { display:none; }

    .b_breadcrumb{
        display: inline-block;
        width:100%;
    }
    .col_20{
        float: left;
        width: 20%;
    }
    .g_image{
        text-align: center;
    }
	.b_a_paginator{
        text-align: center;
		background-color: #EFEDED;
		line-height: 1.1em;
		margin-bottom: 3px;
		margin-left: 0.8em;
		padding: 0.3em 0 0.6em 0.3em;
		position: relative;
		width: 97.5%;			
		font-size: 11px;		
    }
	.b_a_paginator .active{
		color: red;
	}
</style>
<?php
    $skin_url = $this->APP_STATIC_SERVER . '/skins/'. $this->APP_TEMPLATE . '/'. $this->APP_SKIN;
?>
<div class="grid_12">
    <div class="grid_8 alpha">
        <!--sub menu -->
        <?php echo $this->widget('bizprofile','menu',array()); ?>
        <!--end sub menu -->

        <?php if(!$this->found):?>
        <?php echo $this->translator('not_found_database');?>
        <?php else:?>
        <div class="subcolumns pb10">
            <div class="b_breadcrumb b_bottom">
                <div style="width:50%; float:left;">
                    <?php
                        $arrRouteList = array(
                            'enterprise_id' => $this->enterprise->id,
                            'enterprise_name' => Tomato_Utility_String::removeSign($this->enterprise->company_name_en, '-', true)
                        );
                    ?>
                    <a href="<?php echo $this->url($arrRouteList, 'b2b_blog_list');?>"><?php echo $this->translator('blog_list_breadcrumb');?></a> »
                    <?php echo $this->translator('blog_view_breadcrumb');?>
                </div>
                <div style="width:50%; float:left; text-align:right;">
                    <?php
                        /**
                        * Blog content other language
                        **/
                        $arrOtherBlogs = $this->arrOtherBlogs;
                    ?>
                    <?php if(count($arrOtherBlogs)):?>
                        <?php foreach($arrOtherBlogs as $r_blog):?>
                            <?php
                                $arrPro = $r_blog->getProperties();
                                $arrPro['language'] = $this->lang;
                            ?>
                            <?php /**<a href="<?php echo $this->url($arrPro, 'b2b_blog_view');?>/?lang_code=<?php echo $r_blog->language;?>&enterprise_id=<?php echo $this->enterprise->id;?>"><?php echo $this->translator('view_by_lang_'.$r_blog->language);?></a>&nbsp;&nbsp;&nbsp;&nbsp;*/?>
                        <?php endforeach;?>
                    <?php else:?>
                    	<?php if($this->lang=="en_US"):?>
                        <a href="javascript:void(0)" id="auto_translate"><?php echo $this->translator('blog_view_auto_translate');?></a><span id="s_loading"></span>&nbsp;&nbsp;
                        <?php endif;?>
                    <?php endif;?>

                    <?php if($this->allowAddMore && $this->allow('add') && $this->allowAction):?>
                    <?php
                        $arrRouteAdd = array(
                            'enterprise_id' => $this->enterprise->id,
                            'enterprise_name' => Tomato_Utility_String::removeSign($this->enterprise->company_name_en, '-', true)
                        );
                    ?>
                    <a href="<?php echo $this->url($arrRouteAdd, 'b2b_blog_add');?>/?id=<?php echo $this->blog->blog_id;?> "><?php echo $this->translator('blog_add_more_btn');?></a>&nbsp;&nbsp;
                    <?php endif;?>
                    <?php if($this->allow('edit') && $this->allowAction && $this->blog->language == $this->lang):?>
                    <?php
                        /**
                        * Param for url;
                        **/
                        $arrRouteEdit = array(
                            'enterprise_id' => $this->enterprise->id,
                            'enterprise_name' => Tomato_Utility_String::removeSign($this->enterprise->company_name_en, '-', true),
                            'blog_id' => $this->blog->blog_id,
                            'slug' => $this->blog->slug,
                        );
                        ?>
                    <a href="<?php echo $this->url($arrRouteEdit, 'b2b_blog_edit');?>"><?php echo $this->translator('blog_edit_btn');?></a>
                    <?php endif;?>
                </div>
            </div>
            <div class="b_l_content">
                <?php
                    /**
                    * Blog to view
                    **/
                    $blog = $this->blog;

                    /**
                    * Enterprise to view
                    **/
                    $enterprise = $this->enterprise;
                    
					$arrRouteEnt = array(
                            'enterprise_id' => $this->enterprise->id,
                            'enterprise_name' => Tomato_Utility_String::removeSign($this->enterprise->company_name_en, '-', true),
                        );
                ?>
                <div class="title" id="blog_title"><?php echo $blog->title;?></div>
                <div class="b_post">
                    <lable><?php echo $this->translator('blog_post_by');?></lable>
                    <a href="<?php echo $this->url($arrRouteEnt, 'bizprofile_enterprise_index');?>"><?php echo isset($enterprise->company_name_vn)?$enterprise->company_name_vn:null;?></a>
                    (<?php echo date('d-m-Y', strtotime($blog->date));?>)
                </div>
                <div class="b_link"><a href="http://<?php echo $blog->link;?>"><?php echo $blog->link;?></a></div>
                <div class="b_content">
                    <div style="width:16%; float:left; display:inline-block">
                        <?php
                            /**
                            * Check image exist directory
                            **/
                            $file = $this->APP_STATIC_SERVER.'/images/logos/'.$blog->enterprise_id.'.jpg';
                            $path = str_replace($this->APP_STATIC_SERVER,getcwd(),$file);
                            if(!is_file($path)){
                                $file = $this->APP_STATIC_SERVER.'/images/logos/no-logo.jpg';
                            }

                        ?>
                        
                        <img width="100" src="<?php echo $file;?>" />
                    </div>
                    <div id="blog_content" style="width:84%; float:left; display:inline-block;"><?php echo $blog->content;?></div>
                </div>

                <div class="b_action">
                    <?php if($this->user):?>
                    <a href="javascript:void(0)" class="comment_button" id="<?php echo $blog->blog_id;?>"><?php echo $this->translator('blog_comment')?></a>&nbsp;&nbsp;&nbsp;&nbsp;
                    <?php endif;?>
                    <?php if($this->allow('delete') && $this->allowAction):?>
                    <a id="btn_del_blog" href="javascript:void(0)"><?php echo $this->translator('blog_delete_btn');?></a>
                    <?php endif;?>
					
                </div>

                <div style="padding-left:75px;">
                    <ol id="update" class="timeline">
                        <div id="outPut">
                        <?php foreach($this->comments as $comment):?>
                        <li id="comment_content_<?php echo $comment->id;?>">
                            <div style="float:left; width:45px;">
                                <?php
									
                                    /**
                                    * User of commment to view
                                    **/
                                    $cUI = isset($this->arrUserCInfo[$comment->id])?$this->arrUserCInfo[$comment->id]:null;
									$showLink  = false;
                                    if(isset($cUI['enterprise']->id))
										if($cUI['enterprise']->frontend_active == 1 && $cUI['enterprise']->deleted != 1) 
											$showLink = true;

									$showStatus = isset($cUI['enterprise']->id)?true:false;
									$deleted = (isset($cUI['enterprise']->deleted) && $cUI['enterprise']->deleted == 1)? true : false;
									$frontend_active  = (isset($cUI['enterprise']->frontend_active) && $cUI['enterprise']->frontend_active == 1)? true : false;
									/**
                                    * Check image exist directory
                                    **/
									if(isset($cUI['enterprise']->id)){
										$file = $this->APP_STATIC_SERVER.'/images/logos/'.$cUI['enterprise']->id.'.jpg';
										$path = str_replace($this->APP_STATIC_SERVER,getcwd(),$file);
										if(!is_file($path)){
											$file = $this->APP_STATIC_SERVER.'/images/logos/no-logo.jpg';
										}									
									}else 
										$file = $this->APP_STATIC_SERVER.'/images/logos/no-logo.jpg';
										
									if($deleted==false && $frontend_active ==true){
                                    /**
					                 * Enterprise route
					                 */
					                $arrRouteEn = array(
					                    'enterprise_id' => $cUI['enterprise']->id,					                    
					                    'enterprise_name' => Tomato_Utility_String::removeSign($cUI['enterprise']->company_name_vn, '-', true),                    
					                );
                                ?>
										<a href="<?php echo $this->url($arrRouteEn, 'bizprofile_enterprise_index');?>">
											<img width="40" src="<?php echo $file;?>" />
										</a> 
										
                               	<?php } else{?>
                               		<!--img width="40" src="<?php // echo $file;?>" /-->
                               		<div style="width: 40px; float: left;"">&nbsp;</div>
                               	<?php }?>
                               	
                            </div>
                            <div align="left" style="float:left; width:475px; font-size:11px;">
								<?php 
									if(isset($cUI['enterprise']->id)){
										$arrRouteEn = array(
											'enterprise_id' => $cUI['enterprise']->id,					                    
											'enterprise_name' => Tomato_Utility_String::removeSign($cUI['enterprise']->company_name_vn, '-', true),                    
										);							
									}
									if($cUI['user'])
									{
										$arrUser = array(
					                    'user_name' => $cUI['user']->user_name,					                    
					                		);
									}									
								?>
                            	<?php if($deleted==false && $frontend_active ==true){?>
                            		<a href="<?php echo $this->url($arrRouteEn, 'bizprofile_enterprise_index');?>">
	                                	<?php echo ($cUI['user']->full_name)?$cUI['user']->full_name:$cUI['user']->user_name;?>
	                                	<?php echo ($cUI['enterprise'])?', '.$cUI['enterprise']->company_name_vn:null;?>
	                               	</a>
                            	<?php }else{?>
                            		<?php if($cUI['user']):?>
	                            		<a href="<?php echo $this->url($arrUser, 'customer_customer_list');?>">
		                            		<?php if($cUI['user']) echo (isset($cUI['user']->full_name))?$cUI['user']->full_name:$cUI['user']->user_name; ?>
										</a>
									<?php endif;?>
                            	<?php }?>
                            	
                               	<br/>
                                <?php echo $comment->content;?>
                                <br/>
                                <label style="font-size:10px; color:#555555;"><?php echo date('h:i:s d/m/Y', strtotime($comment->date));?></label>
                            </div>
                            <?php if(@$this->user->enterprise->id == $blog->enterprise_id):?>
                            <div style="float:left; width:10px; margin-top:-5px; padding-left:5px;">
                                <a href="javascript:void(0)" class="del_comment" rel="<?php echo $comment->id;?>">
                                    <b>x</b>
                                </a>
                            </div>
                            <?php endif;?>							
							<?php if($showStatus):?>
								<?php if($deleted):?>
									<span style="margin-left:42px;color:red;font-size:11px;"><?php echo $this->translator("blog_view_enterprise_non_exist");?></span>
								<?php endif; ?>
								<?php if(!$deleted && !$frontend_active):?>
									<span style="margin-left:42px;color:#BC8F8F;font-size:11px;"><?php echo $this->translator("blog_view_enterprise_inactive");?></span>
								<?php endif; ?>
							<?php endif;?>
                        </li>
                        <?php endforeach;?>						
                        </div>
						<!-- 
							Name : Cong Ngo
							Date  : 05/05/2011
							Phân trang bằng jquery dữ liệu đã được load lên hết rồi,
							chỉ ẩn đi và cho hiện lên thôi.
						-->
						<div class="b_a_paginator">
						</div>
						<script language="javascript">							
							function showItem(offset,count){
								$("#outPut").find("li").hide();
								var i = 0;
								$("#outPut").find("li").each(function(){
									if(i>=offset && i < offset+count)
									   $(this).show();
									i++;
								});
							}
							function getOffset(page,perPage){
								return (perPage * (page-1) < 0)? 0 : perPage * (page-1);
							}
							function getMaxPage(){
								var total  = $("#outPut").find("li").size();
								return Math.floor(total/<?php echo $this->perPage?>) + ((total % <?php echo $this->perPage?> != 0)?1:0);
							}
							function registerPerpage(currentPage){								
								var maxPage = getMaxPage();
								var html = '';
								if(maxPage > 1){									
									for(var i =1;i <= maxPage; i++){
										var classs = "";
										if(i == currentPage) classs = "active";										
										html += '<a href="#" class="' + classs + '" name="page-'+i+'">'+i+'</a>&nbsp;';									
									}
									$(".b_a_paginator").html(html);
									var offset = getOffset(currentPage,<?php echo $this->perPage?>);
									showItem(offset,<?php echo $this->perPage?>);
									$(".b_a_paginator").find("a").unbind("click");
									$(".b_a_paginator").find("a").bind("click",function(){
										if($(this).hasClass("active")==false){
											$(".b_a_paginator").find(".active").removeClass("active");
											$(this).addClass("active");										
											var page = $(this).attr("name") + "";
											var page = page.replace("page-","");
											var offset  =  getOffset(page,<?php echo $this->perPage?>);										
											showItem(offset,<?php echo $this->perPage?>);										
										}
										return false;
									});
									$(".b_a_paginator").find("a").removeClass("active");
									$(".b_a_paginator").find("a:eq("+(currentPage-1)+")").addClass("active");
									$(".b_a_paginator").show();
								}else{
									$(".b_a_paginator").hide();
								}
							}
							$(document).ready(function(){
								registerPerpage(<?php echo $this->pageIndex?>);
							});
						</script>						
						<!--
							******** END SCRIPT *************
						-->
						
                        <div class='panel' id="slidepanel<?php echo $blog->blog_id;?>">
                            <li style="padding-left:4px; height: 50px;">
                                <form action="<?php echo $this->url(array(), 'b2b_blog_comment');?>" method="post" id="blogCommentForm">
                                    <input type="hidden" value="<?php echo $blog->blog_id;?>" name="blog_id" />
                                    <input type="hidden" value="<?php echo isset($this->user)?$this->user->user_id:null;?>" name="user_id" />
                                    <input type="hidden" value="<?php echo $blog->enterprise_id;?>" name="enterprise_id" />

                                    <div style="float:left; width:45px">
                                        <?php
                                            /**
                                            * Check image exist directory
                                            **/
											//var_dump($this->user->enterprise->id);die;
                                            $file = $this->APP_STATIC_SERVER.'/images/logos/'.@$this->user->enterprise->id.'.jpg';
                                            $path = str_replace($this->APP_STATIC_SERVER,getcwd(),$file);
                                            if(!is_file($path)){
                                                $file = $this->APP_STATIC_SERVER.'/images/logos/no-logo.jpg';
                                            }

                                        ?>
                                        <img width="40" src="<?php echo $file;?>"/>
                                    </div>
                                    <div align="left" style="float:left; width:485px; margin-bottom:3px; ; padding-top:3px;">
                                        <textarea style="width:100%;height:23px" name="content" id="content"></textarea>
                                    </div>
                                    <div style="text-align:right; padding-right:5px; padding-top:5px;">
                                        <input type="button" value="Comment" id="comment_submit" />
                                    </div>
                                </form>
                            </li>
                        </div>
                    </ol>
                </div>
            </div>
        </div>

        <div id="messageDialog"></div>
        <div id="confirmDialog"></div>

        <script type="text/javascript" src="<?php echo $this->APP_STATIC_SERVER; ?>/js/jquery.form/jquery.form.js"></script>
        <script src="<?php echo $skin_url; ?>/html/js/jquery.translate.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(document).ready(function() {

                /**
                * Auto translate
                */
                $("#auto_translate").click(function(){
                    $("#s_loading").addClass('t_a_ui_helper_loading');
                    $("#blog_title").translate('en');
                    $("#blog_content").translate('en',{
                                        complete:function(){
                                            //console.log( this.translation );
                                            $('#s_loading').removeClass('t_a_ui_helper_loading');
                                        }}

                            );
                })

                /**
                * Comment show or close form
                */
                $(".comment_button").click(function(){
                    var element = $(this);
                    var I = element.attr("id");

                    $("#slidepanel"+I).slideToggle(300);
                    $(this).toggleClass("active");
                    $('#content').focus();

                    return false;
                });

                flag = '<?php echo $this->flag;?>';
                blog_id = '<?php echo $blog->blog_id?>';
                if(flag == 'true'){
                    $("#slidepanel"+blog_id).slideToggle(300);
                    $(this).toggleClass("active");
                    $('#content').focus();
                }


                /**
                * Delete blog
                */
               $('#btn_del_blog').click(function(){
                    var self = this;
                    $('#confirmDialog').html('<?php echo addslashes($this->translator('blog_view_action_delete_blog_confirm')); ?>')
                        .dialog({
                            title: '<?php echo addslashes($this->translator('common_dialog_confirm')); ?>',
                            dialogClass: 't_a_ui_dialog',
                            modal: true,
                            closeOnEscape: false,
                            resizable: false,
                            open: function(event, ui) {
                                $('.ui-dialog-titlebar-close').hide();
                                $(this).parent().find('.ui-dialog-buttonpane button').wrapInner('<span/>');
                            },
                            buttons: {
                                '<?php echo addslashes($this->translator('common_dialog_confirm_no')); ?>': function() {
                                    $(this).dialog('destroy');
                                },
                                '<?php echo addslashes($this->translator('common_dialog_confirm_yes')); ?>': function() {
                                    $(this).dialog('destroy');
                                    window.location = '<?php echo $this->url(array('enterprise_id' => $this->enterprise->id,'blog_id' => $this->blog->blog_id), 'b2b_blog_delete');?>';
                                }
                            }
                    });
               });
               /**End delete comment*/

                /**
                * Ajax submit form
                */
                var options = {
                    //target: '#outPut', // target element(s) to be updated with server response
                    beforeSubmit:  showRequest,
                    url: '<?php echo $this->url(array(), 'b2b_blog_comment');?>',
                    type: 'POST',  // pre-submit callback,
                    success: function(msg){
						$("#outPut").html(msg);
                        $('#content').attr('value', '');
                        $('#content').focus();
						registerPerpage(getMaxPage());
                    }
                };
                $('#comment_submit').click(function(){
                    content = $('#content').val();
                    content = content.replace(/^\s+|\s+$/g, '');
                    if(content != ''){
                        $("#blogCommentForm").ajaxSubmit(options);
                    }else{
                        $(".comment_button").removeClass('active');

                        var I = $(".comment_button").attr("id");
                        $("#slidepanel"+I).slideToggle(300);
                        $(this).toggleClass("active");

                        return false;
                    }
                });

                /**
                * Delete comment
                */
                $('a.del_comment').click(function() {
                var self = this;
                $('#confirmDialog').html('<?php echo addslashes($this->translator('blog_view_action_delete_comment_confirm')); ?>')
                    .dialog({
                        title: '<?php echo addslashes($this->translator('common_dialog_confirm')); ?>',
                        dialogClass: 't_a_ui_dialog',
                        modal: true,
                        closeOnEscape: false,
                        resizable: false,
                        open: function(event, ui) {
                            $('.ui-dialog-titlebar-close').hide();
                            $(this).parent().find('.ui-dialog-buttonpane button').wrapInner('<span/>');
                        },
                        buttons: {
                            '<?php echo addslashes($this->translator('common_dialog_confirm_no')); ?>': function() {
                                $(this).dialog('destroy');
                            },
                            '<?php echo addslashes($this->translator('common_dialog_confirm_yes')); ?>': function() {
                                $(this).dialog('destroy');
                                /*$(self).html('').addClass('t_a_ui_helper_loading');*/
                                var id = $(self).attr('rel');
                                $.ajaxq('comment', {
                                    type: 'POST',
                                    url: "<?php echo $this->url(array(), 'b2b_blog_comment_delete'); ?>",
                                    data: { id: id },
                                    success: function(response) {
                                        if ('RESULT_OK' == response) {
                                            $('#comment_content_' + id).fadeOut('slow');
                                        }
                                    }
                                });
                            }
                        }
                    });
                });/** end delete*/

            });

            function showRequest(formData, jqForm, options) {
                var queryString = $.param(formData);
                return true;
            }
        </script>
        <?php endif;?>
    </div>
    <div class="grid_4 omega">
        <?php echo $this->widget('bizprofile','map',array()); ?>
        <?php echo $this->widget('bizprofile','help',array()); ?>
    </div>
</div>
<div class="grid_12">
    <?php echo $this->widget('bizprofile','contact',array()); ?>
</div>